package cn.tinlan.shopcoin.app.car.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.SectionEntity;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.app.car.model.ShopCarItemModel;
import cn.tinlan.shopcoin.app.discover.model.DiscoverItemDataModel;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;

/**
 * Created by Mortimer on 2018/11/24. 16:20
 * 佛祖🙏，永无bug。
 */
public class ShopCarRVAdapter extends BaseSectionQuickAdapter<ShopCarItemModel, BaseViewHolder> {

    private OnShopHeadClickListener onmShopHeadClickListener;
    public interface  OnShopHeadClickListener {
      void onShopHeadClick(View view, String shopid);
      void onShopItemClick(ShopCarItemModel.ShopCarItemBean item, int position);
    }

    public void setOnmShopHeadClickListener(OnShopHeadClickListener onmShopHeadClickListener) {
        this.onmShopHeadClickListener = onmShopHeadClickListener;
    }

    public ShopCarRVAdapter(int layoutResId, int sectionHeadResId, List<ShopCarItemModel> data) {
        super(layoutResId, sectionHeadResId, data);
    }
    @Override
    protected void convertHead(BaseViewHolder helper, final ShopCarItemModel item) {
        helper.setText(R.id.shopitem_head_title,item.header);
        ImageView headImg = helper.getView(R.id.shopitem_head_selectimg);
        if (item.isShopSelect) {
            headImg.setImageResource(R.drawable.select_hlight);
        }else {
            headImg.setImageResource(R.drawable.select_normal);
        }
        headImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onmShopHeadClickListener != null){
                    onmShopHeadClickListener.onShopHeadClick(v,item.shopid);
                }
            }
        });
    }

    @Override
    protected void convert(final BaseViewHolder helper, ShopCarItemModel item) {
        final ShopCarItemModel.ShopCarItemBean t = item.t;
        helper.setText(R.id.shopitem_title, t.getName());
        helper.setText(R.id.shopitem_price,"¥"+t.getPrice());
        if (t.isIslastgood()){
            helper.getView(R.id.shopcar_bottom_img).setVisibility(View.VISIBLE);
        }else {
            helper.getView(R.id.shopcar_bottom_img).setVisibility(View.GONE);
        }
        ImageView selectImg = helper.getView(R.id.shopitem_select_img);

        if (t.isSelected()) {
            selectImg.setImageResource(R.drawable.select_hlight);
        }else {
            selectImg.setImageResource(R.drawable.select_normal);
        }
        selectImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onmShopHeadClickListener != null){
//                    Log.d("position",""+helper.getAdapterPosition()+"==="+helper.getLayoutPosition());
                    onmShopHeadClickListener.onShopItemClick(t,helper.getAdapterPosition());
                }
            }
        });


        ImageView reduceIMG = helper.getView(R.id.shopitem_subtract);
        final TextView numTV = helper.getView(R.id.shopitem_num);
        int tempNum = Integer.parseInt(t.getBuy_num());
        if (tempNum < 1)
            tempNum = 1;
        numTV.setText(tempNum+"");
        ImageView addIMG    = helper.getView(R.id.shopitem_add);

        reduceIMG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num1 = numTV.getText().toString();
                int tempNum = Integer.parseInt(num1);
                tempNum--;
                if (tempNum < 1){
                    tempNum = 1;
                    return;
                }
                numTV.setText(tempNum+"");
                t.setBuy_num(tempNum+"");
                editGoodNum(t.getId(),tempNum+"",t);
            }
        });

        addIMG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num1 = numTV.getText().toString();
                int tempNum = Integer.parseInt(num1);
                tempNum++;
                numTV.setText(tempNum+"");
                t.setBuy_num(tempNum+"");
                editGoodNum(t.getId(),tempNum+"",t);
            }
        });

    }

//    @Override
//    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
//
//    }


    private void editGoodNum(String id,String num,ShopCarItemModel.ShopCarItemBean itemBean) {
        EasyHttp.post(Constants.ShoppingCarNumEdit)
                .params("id",id)
                .params("buy_num",num)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

//                        ToastUtils.show("修改商品数量失败");
                    }

                    @Override
                    public void onSuccess(String s) {

//                        ToastUtils.show("修改商品数量成功");
                    }
                }) {
                });
    }
}
